#!/bin/sh

source /AIR_FLASH/script/air.conf
source /AIR_FLASH/system/air_sys.conf
source /AIR_FLASH/script/shcommon

dstNet=$bond_master
bakNet=$bond_back

ping_interval=$ping_max_interval
#log_time=0
ping_time=0
ping_ok=0

function findNet()
{
    tnet=`ifconfig | grep $bond_master | wc -l`
    if [ ${tnet} -eq 1 ] ; then
        dstNet=$bond_master
        bakNet=$bond_back
    fi
    tnet=`ifconfig | grep $bond_back | wc -l`	
    if [ ${tnet} -eq 1 ] ; then
        dstNet=$bond_back
        bakNet=$bond_master
    fi
}
while true 
do
    sleep 1
    if [ -f "/tmp/.poeflg" ] ; then
        ping_time=0
        continue
    fi
    if [[ $ping_ok -eq 1 ]] && [[ -f "/tmp/.bondCheck.tmp" ]] ; then        
        continue;
    fi
    #log_time=$[$log_time+1]
    ping_time=$[$ping_time+1]
    if [ $ping_time -ge $ping_interval ] ; then
        findNet
        i=0
        while [ $i -lt $fping_max ] 
        do
            i=$[$i+1]
            ping -c 1 -W $ping_timeout ${air_server_ip} | grep "64 bytes from $air_server_ip" > /dev/null 2>&1
            if [ $? -eq 0 ]; then
                ifconfig $bakNet down
                ping_interval=$ping_max_interval_OK		
                #if [ $log_time -ge $link_log_max_time ]; then
                    log_time=0
                #	INFO "************"$dstNet" link up"
                #fi
                ping_time=0
                ping_ok=1
                break
            else
                if [ $i -eq $fping_max ] ; then 
                    ifconfig $dstNet down
                    ifconfig $bakNet up
			
                    tNet=$dstNet
                    dstNet=$bakNet
                    bakNet=$tNet
                    ping_interval=$ping_max_interval
                    #if [ $log_time -ge $link_log_max_time ]; then
                    #	log_time=0
                    #		Error "************"$dstNet" link down"
                    #fi		
                fi
                ping_ok=0
	     fi
        done
        ping_time=0
    fi     
done


